#include "battleModel.hpp"
#include <iostream>

using namespace std;

Slot::Slot(
	const int capacity,
	const int idx
)
:	m_capacity(capacity),
	m_idx(idx)
{
}

int Slot::freeCapacity() const
{
	int freeCapacity = m_capacity;
	for (int i=0 ; i<m_units.size() ; i++) {
		freeCapacity -= m_units[i]->m_capacityRequired;
	}
	return freeCapacity;
}

void Slot::print() const
{
	cout<<m_idx<<" ("<<freeCapacity()<<"/"<<m_capacity<<"):\t";
	for (int i=0 ; i<m_units.size() ; i++) {
		m_units[i]->print();
		cout<<" ";
	}
	cout<<endl;
}

BattleModel::BattleModel()
{
	for (int i=-5 ; i<5 ; i++) {
		m_slots.push_back( Slot(10, i) );
	}
}

void BattleModel::print() const
{
	for (int i=0 ; i<m_slots.size() ; i++) {
		m_slots[i].print();
	}
}

void BattleModel::addUnit(
	const int location,
	const std::string name,
	const BWAPI::UnitType type,
	const BattleModelUnitController controller
)
{
	BattleModelUnit *u = new BattleModelUnit(location, type, name, controller);
	if (controller==BattleModelUnitController::Player) {
		m_myUnits.push_back( u );
	} else {
		m_enemyUnits.push_back( u );
	}
	m_slots[location-m_slots[0].m_idx].m_units.push_back( u );
}

void BattleModel::onFrame()
{
	for (int i=0 ; i<m_myUnits.size() ; i++) {
		m_myUnits[i]->onFrame();
	}
	for (int i=0 ; i<m_enemyUnits.size() ; i++) {
		m_enemyUnits[i]->onFrame();
	}
}

int main()
{
	BattleModel bm;
	bm.addUnit(4, "Z1", BWAPI::UnitTypes::Protoss_Zealot, Player);
	for (int t=0 ; t<100 ; t++) {
		cout<<"time "<<t<<"\n";
		bm.print();
		bm.onFrame();
	}
	return 0;
}